<template>
  <div class="U000005-template1" :class="className" :id="`location-box-${this.id}`">
    <!-- style -->
    <div v-html="css"></div>

    <div class="location-box" v-if="datas.location_style == 1 || datas.location_style == 2">
      <div class="location-left" @click="tolocation">
        <i class="iconfont icon-order_locate"></i>
        <span class="title">{{ address || "全国" }}</span>
        <i class="iconfont icon-advertise-next"></i>
      </div>
      <div class="location-right" v-show="datas.show_search != 1" @click="tosearch">
        <i class="iconfont icon-all_search_2"></i>
        <span>{{ datas.search_title }}</span>
      </div>
      <div v-if="datas.location_style == 2 && datas.show_search != 1" @click="tosearch" class="location-search">搜索</div>
    </div>

    <div class="location-box2" v-if="datas.location_style == 3">
      <div class="location-whole">
        <div class="location-left" @click="tolocation">
          <span class="title">{{ address || "全国" }}</span>
          <i class="iconfont icon-advertise-next"></i>
        </div>
        <div class="location-right" v-show="datas.show_search != 1" @click="tosearch">
          <i class="iconfont icon-all_search_2"></i>
          <span>{{ datas.search_title }}</span>
        </div>
      </div>
    </div>
    <div class="location-box-four" v-if="datas.location_style == 4">
      <div class="top">
        <van-icon name="arrow-left" @click="toBack" />
        <div class="imageBox" v-if="datas.logo_image">
          <img :src="datas.logo_image" alt="" />
        </div>
        <div class="locationB" v-if="address" @click="tolocation">
          <i class="iconfont icon-dingwei"></i>
          <div class="text">{{ address || "全国" }}</div>
        </div>
        <div class="locationB" v-else-if="!address" @click="tolocation">
          <i class="iconfont icon-dingwei" v-if="datas.show_location_info == '0'"></i>
          <div class="text">{{ datas.show_location_info == "1" ? "" : "全国" }}</div>
        </div>
      </div>
      <div class="bottom" v-if="datas.show_search != 1" @click="tosearch(true)">
        <i class="iconfont icon-sousuo1"></i>
        <span>{{ datas.search_title }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
// 自定义样式
const css = function() {
  const { preview_color, border_style, search_border_radius, bg_color, border_color, font_color, city_font_color, logo_radius } = this.datas;

  return `
        .component-${this.id} .location-box, .component-${this.id} .location-box2 , .component-${this.id} .location-box-four {
            background-color: ${preview_color.color || "#f8f8f8"};
            background-image: url(${preview_color.isColor == 2 ? preview_color.image : ""});
            background-size: 100% 100%;
            background-position: center;
            background-repeat: no-repeat;
        }
        .component-${this.id} .location-box .location-right{
            border-radius: ${search_border_radius}px;
            border: solid  ${border_style}px ${border_color};
            background-color: ${bg_color || "#f8f8f8"};
            color: ${font_color};
        }

        .component-${this.id} .location-search {
            color: ${city_font_color};
        }

        .component-${this.id} .location-box .location-left {
            color: ${city_font_color};
        }

        .component-${this.id} .location-box2 .location-whole {
            border-radius: ${search_border_radius}px;
            border: solid  ${border_style}px ${border_color};
            background-color: ${bg_color || "#f8f8f8"};
        }

        .component-${this.id} .location-box2 .location-left {
            color: ${city_font_color};
            border-color: ${city_font_color};
        }

        .component-${this.id} .location-box2 .location-right {
            color: ${font_color};
        }
        .component-${this.id} .location-box-four .top .imageBox{
          border-radius: ${logo_radius}px;
        }
        .component-${this.id} .location-box-four .top .imageBox img{
          border-radius: ${logo_radius}px;
        }
        .component-${this.id} .location-box-four .bottom{
          background-color: ${bg_color || "#f8f8f8"};
          border-radius: ${search_border_radius}px;
          border: solid  ${border_style}px ${border_color};
          color: ${font_color};
        }
        .component-${this.id} .location-box-four .bottom .icon-sousuo1{
          color: ${font_color};
        }
        .component-${this.id} .location-box-four .locationB .text{
          color: ${city_font_color};
        }
        .component-${this.id} .location-box-four .locationB .icon-dingwei{
          color: ${city_font_color};
        }
    `;
};

export default {
  props: ["id", "datas", "styles"],
  data() {
    return {
      address: "",
      city: "",
      point: {},

      oldOffsetTop: 0,
      oDiv: {}
    };
  },
  computed: {
    css() {
      return "<style>" + css.call(this) + "</style>";
    },
    className() {
      const name = ["component-wrapper", `component-${this.id}`];
      return name;
    }
  },
  mounted() {
    this.ready(true);
    this.plugin = localStorage.getItem("plugin_setting") ? JSON.parse(localStorage.getItem("plugin_setting")) : {};
  },
  activated() {
    this.ready();
  },
  deactivated() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    ready(reset) {
      this.$store.commit("setReferer", window.location.href);
      this.$nextTick(() => {
        this.oDiv = document.getElementById(`location-box-${this.id}`);
      });
      window.addEventListener("scroll", this.handleScroll);
      this.fun
        .getLocation()
        .then(res => {
          this.point = res.point;
          if (this.datas.ad_name === "1") {
            let { province, city, district } = res.addressComponent;
            this.address = province ? province + city + district : district;
          } else {
            this.address = res.title;
          }
          this.city = res.city;
          if (reset) {
            if (!this.plugin.address_relation?.open_state) return; //没开启城市关联
            if (this.datas.is_open_city_page) {
              let adcode = window.localStorage.getItem("CityPageADCode");
              if (adcode === res.addressComponent.adcode) return;
              if (Number(res.addressComponent.adcode) <= 0 || !res.addressComponent.adcode) return;
              window.localStorage.setItem("CityPageADCode", res.addressComponent.adcode);
              // 判断是否开启城市关联，有则传城市区域编码给装修接口
              // 同时需要和之前记录的区域编码比较，当有变动时，重新请求更新首页装修数据
              this.$parent.tempInfo({ address_code: res.addressComponent.adcode });
            }
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    toBack() {
      this.$router.go(-1);
    },
    tolocation() {
      let query = {};
      if (this.plugin.address_relation?.open_state && this.datas.location_tag) query.tag = this.datas.location_tag;
      this.$router.push(this.fun.getUrl("o2oLocation", {}, query));
    },
    tosearch(key = false) {
      if (!this.city && !key) {
        Toast("请先选择城市");
        return;
      }

      let search_type = this.datas.search_type || "store";
      this.$router.push(this.fun.getUrl("o2oSearchMultiple", { city: this.city, point: JSON.stringify(this.point), fromHome: 1 }, { search_type }));
    },
    handleScroll() {
      if (this.datas.is_top_fixed != 1) {
        return;
      }

      let s = document.body.scrollTop || document.documentElement.scrollTop;
      if (this.oDiv.offsetTop != 0) {
        this.oldOffsetTop = this.oDiv.offsetTop;
      }
      if (s > this.oDiv.offsetTop && s > this.oldOffsetTop) {
        if (this.fun.getPhoneEnv() == 3) {
          this.oDiv.style = "position:fixed;top:0;width:375px;z-index:10";
        } else {
          this.oDiv.style = "position:fixed;top:0;width:100%;z-index:10";
        }
      } else {
        this.oDiv.style = "";
      }
    }
  }
};
</script>

<style lang="scss" scoped>
// 默认
.component-wrapper {
  width: 100%;
  text-align: left;
}

.title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.location-box {
  display: flex;
  height: 45px;
  line-height: 45px;
  padding: 0 10px;

  .location-left {
    width: 40%;
    display: flex;
  }

  .location-right {
    flex: 0 0 47%;
    overflow: hidden;
    height: 25px;
    line-height: 25px;
    align-self: center;
    padding: 0 8px;
  }

  .location-search {
    text-align: center;
    flex: 0 0 10%;
    margin-left: 5px;
  }
}

.location-box2 {
  height: 45px;
  line-height: 45px;
  width: 100%;
  padding: 8px 10px;

  .location-whole {
    display: flex;
    padding: 5px;
    height: 30px;
    line-height: 20px;
  }

  .location-left {
    display: flex;
    width: 40%;
  }

  .location-right {
    border-left: 1px solid #d3d3d3;
    width: 60%;
    padding-left: 10px;
  }
}
.location-box-four {
  padding: 0.5rem 0.75rem;
  box-sizing: border-box;
  background: #fff;
  .bottom {
    height: 1.75rem;
    padding: 0.38rem 0.84rem;
    box-sizing: border-box;
    background: #f6f6f6;
    border-radius: 1.09rem;
    margin-top: 0.5rem;
    display: flex;
    align-items: center;
    .inp {
      border: none;
      flex: 1;
    }
    .icon-sousuo1 {
      flex-shrink: 0;
      color: #999999;
      margin-right: 0.25rem;
    }
  }
  .top {
    display: flex;
    align-items: center;
    .imageBox {
      height: 1.875rem;
      background: #f0f0f1;
      margin-left: 4px;
      margin-right: 4px;
      img {
        min-width: 1.25rem;
        display: inline-block;
        // width: 100%;
        height: 100%;
      }
    }
    .locationB {
      display: flex;
      align-items: center;
      flex: 1;
      overflow: hidden;
      .icon-dingwei {
        font-size: 1rem;
        font-weight: bold;
        margin-right: 0.25rem;
      }
      .text {
        flex: 1;
        font-size: 0.81rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
}
</style>
